/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Frames.Cheques;

import Clases.MFecha;
import Entities.Cheques;
import Entities.ChequesCondicion;
import Entities.Clientes;
import Frames.PANEL;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author Victor
 */
public class FRMCheques extends javax.swing.JFrame {
    MFecha Fecha = new MFecha();
    
    Controller.ChequesJpaController JPACheque = new Controller.ChequesJpaController(PANEL.EM);
    Controller.ChequesCondicionJpaController JPACondicion = new Controller.ChequesCondicionJpaController(PANEL.EM);
    Controller.ClientesJpaController JPACliente = new Controller.ClientesJpaController(PANEL.EM);
    private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(FRMCheques.class);
    Clientes idCliente=null;
    int idCheque = 0;
    List<ChequesCondicion> listacondicion = new ArrayList<>(); 
    
    /**
     * Creates new form FRMCheques
     */
    public FRMCheques() {
        initComponents();
    }

    public FRMCheques(int idCheque) {        
        this.idCheque = idCheque;
        Image icon = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/Images/pagos.png"));
        this.setIconImage(icon);   
        initComponents();
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtidcliente = new javax.swing.JTextField();
        btnbuscarcliente = new javax.swing.JButton();
        txtcliente = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        txtbanco = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtplaza = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtobservaciones = new javax.swing.JTextArea();
        jLabel8 = new javax.swing.JLabel();
        cmbcondicion = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        txtnumero = new javax.swing.JFormattedTextField();
        fechalibramiento = new javax.swing.JFormattedTextField();
        fechacobro = new javax.swing.JFormattedTextField();
        txtimporte = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        cmbguardar = new javax.swing.JButton();
        btncancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cheques");
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Cliente");

        txtidcliente.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtidclienteKeyReleased(evt);
            }
        });

        btnbuscarcliente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/find.png"))); // NOI18N
        btnbuscarcliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnbuscarclienteActionPerformed(evt);
            }
        });

        txtcliente.setEditable(false);
        txtcliente.setBackground(new java.awt.Color(255, 255, 255));

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/new.png"))); // NOI18N

        jLabel2.setText("Banco");

        jLabel3.setText("Plaza");

        txtobservaciones.setColumns(20);
        txtobservaciones.setRows(5);
        jScrollPane1.setViewportView(txtobservaciones);

        jLabel8.setText("Condición");

        cmbcondicion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "OFICIAL", "EXTERNO" }));

        jLabel9.setText("Número");

        txtnumero.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));

        try {
            fechalibramiento.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        fechalibramiento.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                fechalibramientoFocusGained(evt);
            }
        });

        try {
            fechacobro.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        fechacobro.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                fechacobroFocusGained(evt);
            }
        });

        jLabel4.setText("Importe");

        jLabel5.setText("Fecha Libram.");

        jLabel6.setText("Fecha Cobro.");

        jLabel7.setText("Observaciones");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)
                        .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtidcliente, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnbuscarcliente, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtcliente))
                    .addComponent(txtbanco)
                    .addComponent(txtplaza)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(120, 120, 120)
                                .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txtimporte, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE)
                                .addComponent(txtnumero, javax.swing.GroupLayout.Alignment.LEADING))
                            .addComponent(fechalibramiento, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(fechacobro, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmbcondicion, 0, 262, Short.MAX_VALUE))
                    .addComponent(jScrollPane1))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtidcliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1))
                    .addComponent(txtcliente)
                    .addComponent(btnbuscarcliente, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtbanco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtplaza, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jLabel8)
                    .addComponent(txtnumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbcondicion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtimporte, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fechalibramiento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fechacobro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        cmbguardar.setBackground(new java.awt.Color(128, 188, 149));
        cmbguardar.setMnemonic('G');
        cmbguardar.setText("Guardar");
        cmbguardar.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        cmbguardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbguardarActionPerformed(evt);
            }
        });

        btncancelar.setBackground(new java.awt.Color(255, 103, 56));
        btncancelar.setMnemonic('C');
        btncancelar.setText("Cancelar");
        btncancelar.setToolTipText("");
        btncancelar.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btncancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btncancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(cmbguardar, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btncancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btncancelar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cmbguardar))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void cmbguardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbguardarActionPerformed
//        java.sql.Date libramiento = new java.sql.Date(txtfechalibramiento.getDate().getTime()); 
//        java.sql.Date cobro = new java.sql.Date(txtfechacobro.getDate().getTime()); 
        Date libramiento = null;
        Date cobro = null;
        boolean fechascorrectas=false;
        if(Fecha.CorroborarFechaEnMask(fechalibramiento.getText()) && Fecha.CorroborarFechaEnMask(fechacobro.getText())){
            libramiento = Fecha.Formatear(fechalibramiento.getText());
            cobro = Fecha.Formatear(fechacobro.getText());
            fechascorrectas = true;
        }
        if(!fechascorrectas){
            JOptionPane.showMessageDialog(rootPane, "Corrobore las fechas","Atención",JOptionPane.ERROR_MESSAGE);     
        }
        else{
            if(idCliente==null){
                JOptionPane.showMessageDialog(rootPane, "Seleccione un Cliente","Atención",JOptionPane.ERROR_MESSAGE); 
                txtidcliente.grabFocus();
            }
            else{
                if (txtbanco.getText().isEmpty()){
                    JOptionPane.showMessageDialog(rootPane, "Complete los campos","Atención",JOptionPane.ERROR_MESSAGE); 
                    txtbanco.grabFocus();
                }
                else{
                    if(txtnumero.getText().isEmpty()){
                        JOptionPane.showMessageDialog(rootPane, "Complete los campos","Atención",JOptionPane.ERROR_MESSAGE);                         
                        txtnumero.grabFocus();
                    }
                    else{
                        if(txtimporte.getText().isEmpty()){
                            JOptionPane.showMessageDialog(rootPane, "Complete los campos","Atención",JOptionPane.ERROR_MESSAGE);                         
                            txtimporte.grabFocus();
                        }
                        else{
                            Cheques Entity = new Cheques();
                            Entity.setIdCliente(idCliente);
                            Entity.setBanco(txtbanco.getText());
                            Entity.setNumero(txtnumero.getText());
                            Entity.setFecha(new Date());
                            Entity.setPlaza(txtplaza.getText());
                            Entity.setFechalibramiento(libramiento);
                            Entity.setFechacobro(cobro);
                            Entity.setImporte(Double.parseDouble(txtimporte.getText()));
                            Entity.setObservaciones(txtobservaciones.getText());
                            Entity.setIdChequesCondicion(listacondicion.get(cmbcondicion.getSelectedIndex()));                            
                            if(idCheque == 0){
                                Entity.setEstado(1);
                                try {                        
                                    JPACheque.create(Entity);                                                             
                                    JOptionPane.showMessageDialog(rootPane, "Alta Exitosa");
                                    LimpiarCampos();
                                } catch (Exception ex) {
                                    log.error("Error en Alta de Cliente "+ex.getMessage());
                                    JOptionPane.showMessageDialog(rootPane, "Error al crear el registro","Atención",JOptionPane.ERROR_MESSAGE);
                                }
                            }
                            
                            else{
                                Cheques Entityviejo = JPACheque.findCheques(idCheque);
                                Entity.setId(idCheque);
                                Entity.setEstado(Entityviejo.getEstado());
                                try {
                                    JPACheque.edit(Entity);                        
                                    JOptionPane.showMessageDialog(rootPane, "Actualización Exitosa");
                                    this.dispose();
                                } catch (Exception ex) {
                                    log.error("Error al actualizar Cheques "+ ex.getMessage());
                                    JOptionPane.showMessageDialog(rootPane, "Error en la Actualización","Atención",JOptionPane.ERROR_MESSAGE);
                                }
                            }
                        }
                    }
                }
            }
        }
    }//GEN-LAST:event_cmbguardarActionPerformed

    private void LimpiarCampos(){
        idCliente = null;
        idCheque = 0;
        txtidcliente.setText("");
        txtcliente.setText("");
        txtbanco.setText("");
        txtplaza.setText("");
        txtnumero.setText("");
        txtimporte.setText("");
        java.util.Date date = new java.util.Date(); 
        fechalibramiento.setText(Fecha.Formatear(date));
        date.setMonth(date.getMonth()+1);   
        fechacobro.setText(Fecha.Formatear(date));
        txtobservaciones.setText("");
        LlenarCondicion();
        cmbcondicion.setSelectedIndex(0);
     
    }
    
    private void btncancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btncancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btncancelarActionPerformed

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        java.util.Date date = new java.util.Date();
//        txtfechalibramiento.setDate(date); 
        fechalibramiento.setText(Fecha.Formatear(date));
        date.setMonth(date.getMonth()+1);
//        txtfechacobro.setDate(date);   
        fechacobro.setText(Fecha.Formatear(date));
        LlenarCondicion();
        if(idCheque != 0){          
            LlenarCampos();
        }
    }//GEN-LAST:event_formComponentShown

    private void LlenarCampos(){
        try{
            Cheques Entidad = JPACheque.findCheques(idCheque);
            txtidcliente.setText(Entidad.getIdCliente().getId()+"");
            txtcliente.setText(Entidad.getIdCliente().getNombrerazon());
            txtbanco.setText(Entidad.getBanco());
            txtplaza.setText(Entidad.getPlaza());
            txtnumero.setText(Entidad.getNumero());
            txtimporte.setText(Entidad.getImporte()+"");
            fechacobro.setText(Fecha.Formatear(Entidad.getFechacobro()));
            fechalibramiento.setText(Fecha.Formatear(Entidad.getFechalibramiento()));
            txtobservaciones.setText(Entidad.getObservaciones());            
            cmbcondicion.setSelectedItem(Entidad.getIdChequesCondicion().getDescripcion());
            idCliente = Entidad.getIdCliente();        
        }
        catch(Exception ex){
            log.error(ex.getMessage());
        }        
    }
    
    private void btnbuscarclienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnbuscarclienteActionPerformed
        this.DialogBuscarCliente();
    }//GEN-LAST:event_btnbuscarclienteActionPerformed

    private void txtidclienteKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtidclienteKeyReleased
        String codigo = txtidcliente.getText();
        if (evt.getKeyCode() == evt.VK_ENTER) {
            if (!codigo.isEmpty()) {
                BuscarCliente(Integer.parseInt(codigo));
            } else {
                DialogBuscarCliente();
            }
        }
    }//GEN-LAST:event_txtidclienteKeyReleased

    private void fechalibramientoFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_fechalibramientoFocusGained
        fechalibramiento.selectAll();
    }//GEN-LAST:event_fechalibramientoFocusGained

    private void fechacobroFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_fechacobroFocusGained
        fechacobro.selectAll();
    }//GEN-LAST:event_fechacobroFocusGained

    private void BuscarCliente (int id){
        try {
            List<Clientes> lista = JPACliente.ListarPorId(id);
            if (lista.size()>0){
                idCliente = lista.get(0);
                txtcliente.setText(idCliente.getNombrerazon());
                txtidcliente.setText(idCliente.getId()+"");                
                txtbanco.grabFocus();
            }    
            else{
                idCliente = null;
                txtcliente.setText("");                
                DialogBuscarCliente();           
            }
        } catch (Exception ex) {
            log.error(ex.getMessage());
        }
    }
    
    private void DialogBuscarCliente(){
        Frames.Clientes.BuscarClientes buscar = new Frames.Clientes.BuscarClientes(javax.swing.JOptionPane.getFrameForComponent(FRMCheques.this), true);
        buscar.setVisible(true);
        idCliente = Frames.Clientes.BuscarClientes.getSelectedItem();
        if (idCliente != null ){
            txtcliente.setText(idCliente.getNombrerazon());
            txtidcliente.setText(idCliente.getId()+"");
            txtbanco.grabFocus();
        }        
    }
    
    
    private void LlenarCondicion(){        
        try {
            cmbcondicion.removeAllItems();
            listacondicion = JPACondicion.Listar();
            int i =0;
            for (i=0;i<listacondicion.size();i++){                               
                cmbcondicion.addItem(listacondicion.get(i).getDescripcion());
            }            
        } catch (Exception ex) {
            log.error(ex.getMessage());
        }
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FRMCheques.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FRMCheques.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FRMCheques.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FRMCheques.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FRMCheques().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnbuscarcliente;
    private javax.swing.JButton btncancelar;
    private javax.swing.JComboBox cmbcondicion;
    private javax.swing.JButton cmbguardar;
    private javax.swing.JFormattedTextField fechacobro;
    private javax.swing.JFormattedTextField fechalibramiento;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField txtbanco;
    private javax.swing.JTextField txtcliente;
    private javax.swing.JTextField txtidcliente;
    private javax.swing.JTextField txtimporte;
    private javax.swing.JFormattedTextField txtnumero;
    private javax.swing.JTextArea txtobservaciones;
    private javax.swing.JTextField txtplaza;
    // End of variables declaration//GEN-END:variables
}
